<?php

namespace app\admin\controller;

use think\Controller;


class Admin extends Controller
{
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Admin')->order('update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Admin')->order('update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $return['data'] = $data['data'];
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }
    //删除用户
        public function deleteAdmin(){

            $page = input('delete.page');
            $size = input('delete.size');

           $adminId = input('delete.id');
           $userInfo = model('Admin')->where('id',$adminId)->find();
           if (!$userInfo){
               $return = $this->page($page,$size,false,"查无此人");
           }else{
               $result = model('Admin')->deleteAdmin($userInfo);
               if ($result==1){
                   $return = $this->page($page,$size,true,"删除成功");
               }else{
                   $return = $this->page($page,$size,true,$result);
               }
            }
            return json($return);
        }
    //添加用户
    public function addAdmin(){

        $page = input('post.page');
        $size = input('post.size');

            $data = [
                'username' => input('post.username'),
                'nickname' => input('post.name'),
                'telephone' => input('post.phone'),
                'is_super' => 0,


            ];
            $power = explode(',',str_replace('"','',input('post.item')) );


            $result =model('Admin')->addAdmin($data,$power);
            if ($result==1){

                $return = $this->page($page,$size,true,"添加成功");
            }else{
                $return = $this->page($page,$size,true,$result);
            }


        return json($return);

    }
    //修改用户信息
    public function updateAdmin(){
        $page = input('post.page');
        $size = input('post.size');

           $data = [
               'username' => input('put.username'),
               'nickname' => input('put.name'),
               'telephone' => input('put.phone'),
               'is_super' => 0,
               'update_time' => time()
           ];

           $id = input('put.id');
           $userInfo =model('Admin')->where('id',$id)->find();
           if ($userInfo){
               $result = model('Admin')->updateAdmin($id,$data);
               if ($result){
                   $return = $this->page($page,$size,true,"更新成功");
               }else{
                   $return = $this->page($page,$size,true,$result);
               }
           }else{
               $return = $this->page($page,$size,true, '查无此条用户');
           }
     return json($return);
    }
    //查询用户信息
    public function selectAdmin(){
        $page = input('get.page');
        $size = input('get.size');
        $return = $this->page($page,$size,true,"查询成功");
        return json($return);
    }
    //重置密码
    public function chenge(){


        $newPassword = input('put.newPassword');
        $return = [
            'success' => false,
            'messages' => '更新失败',

        ];
        $data =[
            'username' => input('put.username'),
            'password' => input('put.password')
        ] ;

        $userInfo = model('Admin')->where($data)->find();

        if ($userInfo){
            $result = model('Login')->where('username',$userInfo['username'])->update(['password'=>md5($newPassword)]);

           if ($result){

               $return['messages'] = '更改成功';
               $return['success'] = true;

           }
        }else{
            $return['messages'] = '查无此人';
        }

    return json($return);
    }
    //初始化密码
    public function passwordInit(){

    }

}
